﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using FirstPerson.Objects;
using Microsoft.Xna.Framework.Net;
using ERF;
using ERF.Physics;
using FirstPerson.States;

namespace FirstPerson {
    /// <summary>
    /// Handles the players
    /// </summary>
    public class PlayerHandler : ERF.PlayerHandler {
        private HumanPlayer humanPlayer;

        public event EventHandler<PlayerDieEventArgs> PlayerDie;
        public event EventHandler PlayerSpawn;
        public event EventHandler<PlayerKillEventArgs> PlayerKill;

        public HumanPlayer HumanPlayer
        {
            get { return this.humanPlayer; }
        }

        private List<Player> playersList = new List<Player>();

        public List<Player> PlayersList
        {
            get { return playersList; }
        }

        public PlayerHandler(ERFGame game) : base(game) { }

        protected void HookPlayer(Player player) {
            player.OnDeath += new EventHandler<PlayerDieEventArgs>(player_OnDeath);
            player.OnSpawn += new EventHandler(player_OnSpawn);
            player.OnKill += new EventHandler<PlayerKillEventArgs>(player_OnKill);
        }
        void player_OnDeath(object sender, PlayerDieEventArgs e) {
            if(this.PlayerDie != null)
                this.PlayerDie(this, e);
        }
        void player_OnSpawn(object sender, EventArgs e) {
            if(this.PlayerSpawn != null)
                this.PlayerSpawn(this, e);
        }
        private void player_OnKill(object sender, PlayerKillEventArgs e) {
            if(this.PlayerKill != null)
                this.PlayerKill(this, e);
        }

        /// <summary>
        /// Creates the player object and add the player to playerList
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void network_GamerJoined(object sender, GamerJoinedEventArgs e) {
            Player player;
            if (e.Gamer.IsLocal)
            {
                player = this.humanPlayer = new HumanPlayer(game, e.Gamer);
            }
            else
            {
                
                player = new NetworkPlayer(game, "Vincent", e.Gamer);
                if(((FirstPerson.NetworkHandler)this.game.NetworkHandler).IsHost)
                    ((FirstPerson.NetworkHandler)this.game.NetworkHandler).SendGameData((LocalNetworkGamer)this.humanPlayer.NetworkGamer, e.Gamer, PlayingState.GetInstance(this.game.StateMachine).GameData);
            }

            this.HookPlayer(player);

            player.Name = e.Gamer.Gamertag;

            playersList.Add(player);

            e.Gamer.Tag = player;
        }
        /// <summary>
        /// kills the player object when the player left
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void network_GamerLeft(object sender, GamerLeftEventArgs e) {
            Player p = (Player)e.Gamer.Tag;

            p.Dispose();
        }
    }
}
